package arina.university.controller;

import arina.university.dao.GroupDAO;
import arina.university.dao.factory.DAOFactory;
import arina.university.model.Group;
import arina.university.model.Student;
import arina.university.view.GroupView;


import java.util.Collection;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 26.11.13
 * Time: 16:08
 * To change this template use File | Settings | File Templates.
 */
public class GroupController {
    private GroupView view;

    public GroupController(GroupView view) {

        this.view = view;
    }

    DAOFactory memoryFactory = DAOFactory.getDAOFactory(DAOFactory.MEMORY);
    GroupDAO dao = memoryFactory.getGroupDAO();


    public Group createGroup(int id, String s) {

        try {
            Group group = dao.create(id, s);
            return group;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }

    }


    public void removeGroup(int id) {

        try {
            dao.delete(id);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public Group getGroup(int id) {
        try {
            return dao.find(id);
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }

    }

    public Collection<Group> getAllGroups() {
        try {
            return dao.findAll();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    public void findGroup(int id) {
        try {
            Group group = dao.find(id);

            view.getGroupInfo(group);
        } catch (Exception e) {
            System.out.println(e);
        }


    }

    public void updateGroup(int id, String speciality ) {
        try {
            Group group = dao.find(id);

            dao.delete(group.getNumber());
            group.setNumber(id);
            group.setSpeciality(speciality);
            dao.save(group);
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    public void findAllGroups() {
        try {
            Collection<Group> groups = dao.findAll();
            view.getGroupsInfo(groups);
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    public void addStudent(int id,Student student){
         try{
             Group group = dao.find(id);
             group.addStudent(student);

         }
         catch (Exception e){
             System.out.println(e);
         }
    }

    public void deleteStudent(int groupId, Student student){
        try{
           Group group=dao.find(groupId);
           group.getStudents().remove(student);
        }
        catch (Exception e){
            System.out.println(e);
        }
    }


}
